package com.lc1993929.tuling;

/**
 * Created by LiuChang on 2017/5/5.
 */

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

/**
 * Http post请求类
 */
public class HttpPostRequest {

    public static String post(List<NameValuePair> params) {
        HttpPost request = new HttpPost("http://www.tuling123.com/openapi/api");
        try {
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
            request.setEntity(urlEncodedFormEntity);

            HttpResponse response = HttpClients.createDefault().execute(request);

            //根据返回码判断返回是否成功
            String result = "";
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
